import { createApp } from 'vue'
import Notification from '@/components/common/Notification.vue'

class NotificationManager {
  constructor() {
    this.instance = null
    this.container = null
    this.init()
  }

  init() {
    // 创建容器
    this.container = document.createElement('div')
    this.container.id = 'notification-container'
    document.body.appendChild(this.container)

    // 创建Vue实例
    const app = createApp(Notification)
    this.instance = app.mount(this.container)
  }

  success(message, title) {
    return this.instance.success(message, title)
  }

  error(message, title) {
    return this.instance.error(message, title)
  }

  warning(message, title) {
    return this.instance.warning(message, title)
  }

  info(message, title) {
    return this.instance.info(message, title)
  }

  clear() {
    return this.instance.clearAll()
  }

  destroy() {
    if (this.container && this.container.parentNode) {
      this.container.parentNode.removeChild(this.container)
    }
  }
}

// 创建全局实例
const notificationManager = new NotificationManager()

// 插件安装函数
export default {
  install(app) {
    app.config.globalProperties.$notification = notificationManager
    app.provide('notification', notificationManager)
  }
}

// 直接导出实例供非组件使用
export { notificationManager as notification }
